#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''
# Author:linjinting for 02010166
@file: mysql_pool.py
@time: 2020/7/8 11:46
'''


import MySQLdb
from DBUtils.PooledDB import PooledDB
from MySQLdb.cursors import DictCursor
from MySQLdb import MySQLError


class MysqlPool(object):
    __pool = None

    def __init__(self, **config):
        self.pool = self.__create_pool(**config)

    @staticmethod
    def __create_pool(**config):
        if MysqlPool.__pool is None:
            # mysql_log.info("create pool")
            MysqlPool.__pool = PooledDB(creator=MySQLdb, mincached=1, maxcached=5,
                                        **config, use_unicode=False, cursorclass=DictCursor)

        return MysqlPool.__pool

    def getConn(self):
        return self.pool.connection()

